﻿using FastUtil.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FastUtil.Base
{
    public class FormatValidateUtil : BaseUtil
    {
        #region 手机号码验证
        /// <summary>
        /// 是否手机号码
        /// </summary>
        /// <param name="value">手机号码</param>
        /// <returns></returns>
        public static bool IsMobileNumber(string Phone)
        {
            if (string.IsNullOrEmpty(Phone))
            {
                return false;
            }
            Phone = Phone.Trim().Replace("^", "").Replace("$", "");
            /**
             * 手机号码: 
             * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
             * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
             * 联通号段: 130,131,132,155,156,185,186,145,176,1709
             * 电信号段: 133,153,180,181,189,177,1700
             */
            Regex reg = new Regex(@"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$");
            return reg.IsMatch(Phone);

        }
        /// <summary>
        /// 是否中国移动号码
        /// </summary>
        /// <param name="value">手机号码</param>
        /// <returns></returns>
        public static bool IsChinaMobilePhone(string Phone)
        {
            if (string.IsNullOrEmpty(Phone))
            {
                return false;
            }
            /**
             * 中国移动：China Mobile
             * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
             */
            Regex reg = new Regex(@"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\d{8}$)|(^1705\d{7}$)");
            return reg.IsMatch(Phone);
        }
        /// <summary>
        /// 是否中国联通号码
        /// </summary>
        /// <param name="value">手机号码</param>
        /// <returns></returns>
        public static bool IsChinaUnicomPhone(string Phone)
        {
            if (string.IsNullOrEmpty(Phone))
            {
                return false;
            }
            /**
             * 中国移动：China Mobile
             * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
             */
            Regex reg = new Regex(@"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\d{8}$)|(^1709\d{7}$)");
            return reg.IsMatch(Phone);
        }
        /// <summary>
        /// 是否中国电信号码
        /// </summary>
        /// <param name="value">手机号码</param>
        /// <returns></returns>
        public static bool IsChinaTelecomPhone(string Phone)
        {
            if (string.IsNullOrEmpty(Phone))
            {
                return false;
            }
            /**
             * 中国移动：China Mobile
             * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
             */
            Regex reg = new Regex(@"(^1(33|53|77|8[019])\d{8}$)|(^1700\d{7}$)");
            return reg.IsMatch(Phone);
        }
        #endregion

        #region 身份证验证
        public static bool IsIdentificationCard(string Id)
        {
            if (Id.Length == 18)
            {
                bool check = CheckIDCard18(Id);
                return check;
            }
            else if (Id.Length == 15)
            {
                bool check = CheckIDCard15(Id);
                return check;
            }
            else
            {
                return false;
            }
        }
        private static bool CheckIDCard18(string Id)
        {
            long n = 0;
            if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证  
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证  
            }

            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证  
            }

            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = Id.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
            {
                return false;//校验码验证  
            }
            return true;//符合GB11643-1999标准  
        }
        private static int DivRem(int a, int b, out int result)
        {
            result = a % b;
            return (a / b);
        }
        private static bool CheckIDCard15(string Id)
        {
            long n = 0;
            if (long.TryParse(Id, out n) == false || n < Math.Pow(10, 14))
            {
                return false;//数字验证  
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证  
            }
            string birth = Id.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证  
            }
            return true;//符合15位身份证标准  
        }
        /// <summary>
        /// 20170812
        /// ypeng
        /// 15位身份证转为标准18位身份证
        /// </summary>
        /// <param name="idCard"></param>
        /// <returns></returns>
        public static string ConvertIdentificationCard15to18(string idCard)
        {
            string code = idCard.Trim();//获得身份证号码
            if (code.Length == 15)//如果是15位则转换
            {
                char[] strJY = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
                int[] intJQ = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
                string strTemp;
                int intTemp = 0;
                strTemp = code.Substring(0, 6) + "19" + code.Substring(6);
                for (int i = 0; i <= strTemp.Length - 1; i++)
                {
                    intTemp = intTemp + int.Parse(strTemp.Substring(i, 1)) * intJQ[i];
                }
                intTemp = intTemp % 11;
                return strTemp + strJY[intTemp];
            }
            else
            {
                if (code.Length == 18)//如果是18位直接返回
                {
                    return code;
                }
                return string.Empty;//如果即不是15位也不是18位则返回空
            }
        }
        /// <summary>
        /// 20170812
        /// ypeng
        /// 根据身份证计算生日
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static string GetBirthdayByIdentificationCard(string ID)
        {
            ID = ConvertIdentificationCard15to18(ID);
            return new DateTime(Convert.ToInt32(ID.Substring(6, 4)), Convert.ToInt32(ID.Substring(10, 2)), Convert.ToInt32(ID.Substring(12, 2))).ToString();
        }
        /// <summary>
        /// 20170812
        /// ypeng
        /// 根据身份证获取性别
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsReturnValue">是否返回值</param>
        /// IsReturnValue fasle 男/女 true 1/2
        /// <returns></returns>
        public static string GetGenderByIdentificationCard(string ID, bool IsReturnValue)
        {
            if (IsReturnValue)
            {
                ID = ConvertIdentificationCard15to18(ID);
                return Convert.ToInt32(ID.Substring(16, 1)) % 2 == 0 ? "2" : "1";
            }
            else
            {
                ID = ConvertIdentificationCard15to18(ID);
                return Convert.ToInt32(ID.Substring(16, 1)) % 2 == 0 ? "女" : "男";
            }

        }
        #endregion

        #region 字符验证
        /// <summary>
        /// 是否中文
        /// </summary>
        /// <param name="value">中文</param>
        /// <returns></returns>
        public static bool IsChinese(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            Regex reg = new Regex(@"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
            return reg.IsMatch(value);
        }
        /// <summary>
        /// 是否包含中文
        /// </summary>
        /// <param name="value">中文</param>
        /// <returns></returns>
        public static bool IsContainsChinese(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            Regex reg = new Regex(@"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
            return reg.IsMatch(value);
        }
        /// <summary>
        /// 是否是数字
        /// </summary>
        /// <param name="value">中文</param>
        /// <returns></returns>
        public static bool IsNumber(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            Regex reg = new Regex(@"^\+?[1-9][0-9]*$", RegexOptions.IgnoreCase);
            return reg.IsMatch(value);
        }
        /// <summary>
        /// 是否是小数
        /// </summary>
        /// <param name="value">中文</param>
        /// <returns></returns>
        public static bool IsPointNumber(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            Regex reg = new Regex(@"^[0-9]+([.]{1}[0-9]+){0,1}$", RegexOptions.IgnoreCase);
            return reg.IsMatch(value);
        }
        #endregion

        #region 常用性验证
        /// <summary>
        /// 是否是邮政编码
        /// </summary>
        /// <param name="value">中文</param>
        /// <returns></returns>
        public static bool IsPostalCode(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            Regex reg = new Regex(@"^[1-9]\d{5}$", RegexOptions.IgnoreCase);
            return reg.IsMatch(value);
        }
        /// <summary>
        /// 是否是邮箱
        /// </summary>
        /// <param name="isRestrict">是否使用严格验证</param>
        /// <returns></returns>
        public static bool IsEmail(string value, bool isRestrict)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            string pattern = isRestrict
    ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"
    : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
            Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
            return reg.IsMatch(value);
        }
        #endregion
    }
}
